package com.tycmc.signelectroniccontracts.act.renzheng.qiye;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.baidu.ocr.ui.camera.CameraActivity;
import com.tycmc.signelectroniccontracts.R;
import com.tycmc.signelectroniccontracts.act.renzheng.ActQiyeRenZheng;
import com.tycmc.signelectroniccontracts.logic.renzheng.qiye.PreQiYeXinxi;
import com.tycmc.signelectroniccontracts.tools.base.FraBase;
import com.tycmc.signelectroniccontracts.tools.images.ImageSetting;
import com.tycmc.signelectroniccontracts.tools.images.UrlPath;
import com.tycmc.signelectroniccontracts.tools.other.TextShow;
import com.tycmc.signelectroniccontracts.tools.other.TyTypenum;
import com.tycmc.signelectroniccontracts.tools.permissions.PermissionsTool;
import com.tycmc.signelectroniccontracts.tools.show.ToastShow;
import com.tycmc.signelectroniccontracts.tools.store.SharePre;
import com.tycmc.signelectroniccontracts.tools.store.SharePreName;
import com.tycmc.signelectroniccontracts.tools.times.DataTimer;
import com.tycmc.signelectroniccontracts.ui.PopBottom;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FraQIyexinxi extends FraBase implements PreQiYeXinxi.QiYeXinxiInterface {

    private  String filePath;
    private PreQiYeXinxi pre;

    public FraQIyexinxi() {
        filePath = System.currentTimeMillis() + "";
        pre = new PreQiYeXinxi(this,filePath);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fra_qiyexinxi, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        pre.onResult(requestCode, resultCode, data);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        pre.destory();
    }

    private void initView() {
        TextShow.setTextNeed((TextView) getView().findViewById(R.id.tv_gongsimingcheng_state));
        TextShow.setTextNeed((TextView) getView().findViewById(R.id.tv_leixing_state));
        TextShow.setTextNeed((TextView) getView().findViewById(R.id.tv_chenliriqi_state));
        TextShow.setTextNeed((TextView) getView().findViewById(R.id.tv_zhuceziben_state));
        TextShow.setTextNeed((TextView) getView().findViewById(R.id.tv_yingyeqixian_state));
        TextShow.setTextNeed((TextView) getView().findViewById(R.id.tv_tongyixinyongdaima_state));
        TextShow.setTextNeed((TextView) getView().findViewById(R.id.tv_zhusuo_state));
        TextShow.setTextNeed((TextView) getView().findViewById(R.id.tv_farenxinming_state));
        TextShow.setTextNeed((TextView) getView().findViewById(R.id.tv_farenxinmingshenfenzhenghao_state));
        TextShow.setTextNeed((TextView) getView().findViewById(R.id.tv_kaihuyinhang_state));
        TextShow.setTextNeed((TextView) getView().findViewById(R.id.tv_yinhangmingcheng_state));
        TextShow.setTextNeed((TextView) getView().findViewById(R.id.tv_yinhangdizhi_state));
        TextShow.setTextNeed((TextView) getView().findViewById(R.id.tv_yinhangzhanghao_state));
        TextShow.setTextnotNeed((TextView) getView().findViewById(R.id.tv_jingyingfanwei_state));
        //获取验证码
        getView().findViewById(R.id.tv_getcode).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pre.getcode();
            }
        });
        getView().findViewById(R.id.tv_next).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getUpMap() != null) {
                    pre.upMap(getUpMap());

                }


            }
        });
        getView().findViewById(R.id.iv_add_yingye).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PermissionsTool.requestPermiss(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, new PermissionsTool.PerMissionCallBack() {
                    @Override
                    public void permissIsPass(String[] permission, boolean isPass) {
                        if (isPass) {
                            UrlPath.init();
                            Intent intent = new Intent(getActivity(), CameraActivity.class);
                            intent.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,
                                    UrlPath.imagePath + CameraActivity.CONTENT_TYPE_GENERAL+filePath + ".png");
                            intent.putExtra(CameraActivity.KEY_CONTENT_TYPE, CameraActivity.CONTENT_TYPE_GENERAL);
                            startActivityForResult(intent, TyTypenum.yingyezhizhao);
                        } else {
                            if (permission[0].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                                ToastShow.popupToast("请开启文件存储权限");
                            } else if (permission[0].equals(Manifest.permission.CAMERA)) {
                                ToastShow.popupToast("请开启照相机权限");
                            }
                        }

                    }
                }, ((ActQiyeRenZheng) getActivity()));
            }
        });
        getView().findViewById(R.id.tv_chenliriqi_state).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DataTimer.choseDay(getActivity(), new DataTimer.TimerCallBack() {
                    @Override
                    public void callBack(String time) {
                        ((TextView) getView().findViewById(R.id.tv_chenliriqi)).setText(time);
                    }
                }, ((TextView) getView().findViewById(R.id.tv_chenliriqi)).getText().toString().trim(), "", "");
            }
        });
        getView().findViewById(R.id.tv_kaihuyinhang_state).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PopBottom bottom = new PopBottom(getActivity(), new PopBottom.OnItemListen() {
                    @Override
                    public void itemOnclick(AdapterView<?> parent, int position, View view) {
                        if (parent.getAdapter().getItem(position) instanceof Map) {
                            Map item = (Map) parent.getAdapter().getItem(position);
                            TextView textView = (TextView) getView().findViewById(R.id.tv_kaihuyinhang);
                            textView.setText(TextShow.valueOf(item.get(PopBottom.NAME)));
                            textView.setTag(item.get(PopBottom.NAMEID));
                        }
                    }
                });
                List<Map> list = new ArrayList<>();
                Map map = new HashMap();
                map.put(PopBottom.NAME, "中国建设银行");
                map.put(PopBottom.NAMEID, "1");
                Map map1 = new HashMap();
                map1.put(PopBottom.NAME, "中国光大银行");
                map1.put(PopBottom.NAMEID, "2");
                Map map2 = new HashMap();
                map2.put(PopBottom.NAME, "中国农业银行");
                map2.put(PopBottom.NAMEID, "3");
                list.add(map);
                list.add(map1);
                list.add(map2);
                bottom.addAll(list);
                bottom.show(v);
            }
        });
    }


    public Map getUpMap() {
        Map map = new HashMap();
        String gongsimingcheng = ((TextView) getView().findViewById(R.id.et_gongsimingcheng)).getText().toString().trim();
        String leixing = ((TextView) getView().findViewById(R.id.et_leixing)).getText().toString().trim();
        String chengliriqi = ((TextView) getView().findViewById(R.id.tv_chenliriqi)).getText().toString().trim();
        String zhuceziben = ((TextView) getView().findViewById(R.id.et_zhuceziben)).getText().toString().trim();
        String yingyeqixian = ((TextView) getView().findViewById(R.id.et_yingyeqixian)).getText().toString().trim();
        String tongyixinyongdaima = ((TextView) getView().findViewById(R.id.et_tongyixinyongdaima)).getText().toString().trim();
        String zhusuo = ((TextView) getView().findViewById(R.id.et_zhusuo)).getText().toString().trim();
        String jinyingfanwei = ((TextView) getView().findViewById(R.id.et_jingyingfanwei)).getText().toString().trim();
        String farenxingming = ((TextView) getView().findViewById(R.id.et_farenxinming)).getText().toString().trim();
        String farenshenfenzhenghao = ((TextView) getView().findViewById(R.id.et_farenxinmingshenfenzhenghao)).getText().toString().trim();
        String yinghanmingcheng = ((TextView) getView().findViewById(R.id.et_yinhangmingcheng)).getText().toString().trim();
        String yinghangdizhi = ((TextView) getView().findViewById(R.id.et_yinhangdizhi)).getText().toString().trim();
        String yinghangzhanghao = ((TextView) getView().findViewById(R.id.et_yinhangzhanghao)).getText().toString().trim();
        String etcode = ((TextView) getView().findViewById(R.id.et_code)).getText().toString().trim();
        if (TextShow.isEmpty(gongsimingcheng)) {
            ToastShow.popupToast(getString(R.string.please_input_gongsimingcheng));
            return null;
        }
        if (TextShow.isEmpty(leixing)) {
            ToastShow.popupToast(getString(R.string.please_input_leixing));
            return null;
        }
        if (TextShow.isEmpty(chengliriqi)) {
            ToastShow.popupToast(getString(R.string.please_chose_chenliriqi));
            return null;
        }
        if (TextShow.isEmpty(zhuceziben)) {
            ToastShow.popupToast(getString(R.string.please_input_zhuceziben));
            return null;
        }
        if (TextShow.isEmpty(yingyeqixian)) {
            ToastShow.popupToast(getString(R.string.please_input_yingyeqixian));
            return null;
        }
        if (TextShow.isEmpty(tongyixinyongdaima)) {
            ToastShow.popupToast(getString(R.string.please_input_tongyixinyongdaima));
            return null;
        }
        if (TextShow.isEmpty(zhusuo)) {
            ToastShow.popupToast(getString(R.string.please_input_zhusuo));
            return null;
        }
        if (TextShow.isEmpty(farenxingming)) {
            ToastShow.popupToast(getString(R.string.please_input_farenxinming));
            return null;
        }
        if (TextShow.isEmpty(farenshenfenzhenghao)) {
            ToastShow.popupToast(getString(R.string.please_input_farenxinmingshenfenzhenghao));
            return null;
        }
        TextView tvKaihuyinhang = (TextView) getView().findViewById(R.id.tv_kaihuyinhang);
        if (tvKaihuyinhang.getTag() == null) {
            ToastShow.popupToast("请选择开户行");
            return null;
        }
        if (TextShow.isEmpty(yinghanmingcheng)) {
            ToastShow.popupToast(getString(R.string.please_input_yinhangmingcheng));
            return null;
        }
        if (TextShow.isEmpty(yinghangdizhi)) {
            ToastShow.popupToast(getString(R.string.please_input_yinhangdizhi));
            return null;
        }
        if (TextShow.isEmpty(yinghangdizhi)) {
            ToastShow.popupToast(getString(R.string.please_input_yinhangzhanghao));
            return null;
        }
        if (TextShow.isEmpty(etcode)) {
            ToastShow.popupToast("请输入验证码");
            return null;
        }
        map.put("verificationCode", etcode);
        map.put("clientArchiveNo", SharePre.getString(SharePreName.clientArchiveNo, ""));
        map.put("clientName", gongsimingcheng);
        map.put("companyType", leixing);
        map.put("regDate", chengliriqi);
        map.put("registeredCapital", zhuceziben);
        map.put("businessTerm", yingyeqixian);
        map.put("idcNo", tongyixinyongdaima);
        map.put("address", zhusuo);
        map.put("scopeOfBusiness", jinyingfanwei);
        map.put("cpyRepresentive", farenxingming);
        map.put("cpyRepresentiveIdcNo", farenshenfenzhenghao);
        map.put("bankAccountID", tvKaihuyinhang.getTag());
        map.put("bankAccount", tvKaihuyinhang.getText().toString().trim());
        map.put("bankName", yinghanmingcheng);
        map.put("bankAddress", yinghangdizhi);
        map.put("bankAccountNo", yinghangzhanghao);
        return map;
    }

    @Override
    public void edCode(int number) {
        TextView teCode = (TextView) getView().findViewById(R.id.tv_getcode);
        if (number > 0) {
            teCode.setText(String.format(getString(R.string.replay_getcode), number + ""));
            teCode.setEnabled(false);
        } else {
            pre.destory();
            teCode.setEnabled(true);
            teCode.setText(R.string.repleycode);
        }
    }

    @Override
    public void yingyezhizhao(String path) {
        if (new File(path).exists()) {
            getView().findViewById(R.id.iv_add_yingye).setVisibility(View.GONE);
            getView().findViewById(R.id.iv_upload).setVisibility(View.VISIBLE);
            ImageView ivUpload = (ImageView) getView().findViewById(R.id.iv_upload);
            ImageSetting.urlImage(path, ivUpload);
        } else {
            getView().findViewById(R.id.iv_add_yingye).setVisibility(View.VISIBLE);
            getView().findViewById(R.id.iv_upload).setVisibility(View.GONE);
        }
    }

    @Override
    public void initData(String gongsimingcheng, String leixing, String chengliriqi, String zhuceziben, String yingyeqixian, String tongyixingyongdaima, String zhusuo, String jinyingfanwei, String farenxingming) {
        ((TextView) getView().findViewById(R.id.et_gongsimingcheng)).setText(gongsimingcheng);
        ((TextView) getView().findViewById(R.id.et_leixing)).setText(leixing);
        ((TextView) getView().findViewById(R.id.tv_chenliriqi)).setText(chengliriqi);
        ((TextView) getView().findViewById(R.id.et_zhuceziben)).setText(zhuceziben);
        ((TextView) getView().findViewById(R.id.et_yingyeqixian)).setText(yingyeqixian);
        ((TextView) getView().findViewById(R.id.et_tongyixinyongdaima)).setText(tongyixingyongdaima);
        ((TextView) getView().findViewById(R.id.et_zhusuo)).setText(zhusuo);
        ((TextView) getView().findViewById(R.id.et_jingyingfanwei)).setText(jinyingfanwei);
        ((TextView) getView().findViewById(R.id.et_farenxinming)).setText(farenxingming);
    }

    @Override
    public void next(Map upMap) {

        if (getActivity() instanceof ActQiyeRenZheng) {
            ((ActQiyeRenZheng) getActivity()).showNum(ActQiyeRenZheng.farenxinxi, upMap);
        }
    }


}
